package com.ruoyi.develop.lvDaohuoDetail.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.finance.financeAp.service.IFinanceApService;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.service.impl.GoodsOrderInServiceImpl;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.goodsSaledo.domain.GoodsSaledo;
import com.ruoyi.develop.goodsSaledo.service.IGoodsSaledoService;
import com.ruoyi.develop.lvDaohuo.domain.LvDaohuo;
import com.ruoyi.develop.lvRuku.domain.LvRuku;
import com.ruoyi.develop.lvRuku.mapper.LvRukuMapper;
import com.ruoyi.develop.lvRukumingxi.domain.LvRukumingxi;
import com.ruoyi.develop.lvRukumingxi.service.impl.LvRukumingxiServiceImpl;
import com.ruoyi.develop.lvRukumingxi.utils.RukumingxiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.lvDaohuoDetail.mapper.LvDaohuoDetailMapper;
import com.ruoyi.develop.lvDaohuoDetail.domain.LvDaohuoDetail;
import com.ruoyi.develop.lvDaohuoDetail.service.ILvDaohuoDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 到货明细Service业务层处理
 *
 * @author lmm
 * @date 2024-10-07
 */
@Service
public class LvDaohuoDetailServiceImpl implements ILvDaohuoDetailService {
    @Resource
    private LvDaohuoDetailMapper lvDaohuoDetailMapper;
    @Resource
    private GoodsOrderInServiceImpl goodsOrderInService;
    @Resource
    private LvRukumingxiServiceImpl lvRukumingxiService;
    @Resource
    private IGoodsSaleService iGoodsSaleService;
    @Resource
    private IGoodsSaledoService goodsSaledoService;
    @Resource
    private com.ruoyi.develop.lvRukumingxi.utils.RukumingxiUtils RukumingxiUtils;
    @Resource
    private LvRukuMapper lvRukuMapper;

    @Autowired
    private IFinanceApService financeApService;


    /**
     * 查询到货明细
     *
     * @param id 到货明细主键
     * @return 到货明细
     */
    @Override
    public LvDaohuoDetail selectById(String id) {
        return lvDaohuoDetailMapper.selectById(id);
    }

    /**
     * 查询到货明细列表
     *
     * @param entity 到货明细
     * @return 到货明细
     */
    @Override
    public List<LvDaohuoDetail> selectList(LvDaohuoDetail entity) {
        return lvDaohuoDetailMapper.selectList(entity);
    }

    /**
     * 查询到货明细列表合并
     *
     * @param entity 到货明细
     * @return 到货明细
     */
    @Override
    public List<LvDaohuoDetail> selectListMerge(LvDaohuoDetail entity) {
        return lvDaohuoDetailMapper.selectListMerge(entity);
    }




    @Override
    public List<LvDaohuoDetail> listTuihuo(LvDaohuoDetail entity) {
        return lvDaohuoDetailMapper.listTuihuo(entity);
    }

    @Override
    public List<LvDaohuoDetail> listWeiruku(LvDaohuoDetail entity) {
        return lvDaohuoDetailMapper.listWeiruku(entity);
    }

    /**
     * 新增到货明细
     *
     * @param entity 到货明细
     * @return 结果
     */
    @Override
    public int insert(LvDaohuoDetail entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return lvDaohuoDetailMapper.insert(entity);
    }

    /**
     * 修改到货明细
     *
     * @param entity 到货明细
     * @return 结果
     */
    @Override
    public int update(LvDaohuoDetail entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return lvDaohuoDetailMapper.update(entity);
    }

    /**
     * 批量删除到货明细
     *
     * @param ids 需要删除的到货明细主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return lvDaohuoDetailMapper.deleteByIds(ids);
    }

    /**
     * 删除到货明细信息
     *
     * @param id 到货明细主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return lvDaohuoDetailMapper.deleteById(id);
    }

    @Override
    @Transactional
    public int rukuByDaohuo(LvDaohuo lvDaohuo, List<LvDaohuoDetail> list) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        String codeMaxString = lvRukuMapper.selectMaxCode(today);
        if (StringUtils.isNotBlank(codeMaxString)) {
            // 最大编号+1
            codeMaxString = Long.parseLong(codeMaxString) + 1 + "";
        } else {
            codeMaxString = today + "0001";
        }
        LvRuku lvRuku = new LvRuku();
        lvRuku.setNos(codeMaxString);
        lvRuku.setState(BusinessConstants.SALE_TYPE_9);
        lvRuku.setCreateBy(SecurityUtils.getUsername());
        lvRuku.setCreateTime(DateUtils.getNowDate());
        lvRuku.setRukuDate(new Date());
        lvRuku.setRukuid(SecurityUtils.getUserId());
        lvRuku.setRukupeople(SecurityUtils.getUsername());
        lvRuku.setType(BusinessConstants.TYPE_RUKU);
        lvRuku.setSource(BusinessConstants.RUKU_SOURCE_BUY);
        lvRuku.setDaohuoId(lvDaohuo.getId());
        lvRuku.setDaohuoNos(lvDaohuo.getNos());
        lvRuku.setYwType(lvDaohuo.getYwType());
        lvRuku.setSupplierName(lvDaohuo.getSupplierName());
        lvRuku.setSupplierId(lvDaohuo.getSupplierId());
        lvRuku.setId(IdUtils.fastSimpleUUID());
        lvRuku.setStatus(BusinessConstants.STORAGE_STATE_YES);
//        list  根据 setGoodsBuyTotal 求和
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (LvDaohuoDetail entity : list) {
            totalPrice = totalPrice.add(entity.getTotalPrice());
        }
        lvRuku.setMoney(totalPrice);
        lvRukuMapper.insert(lvRuku);
        for (LvDaohuoDetail entity : list) {
            RukumingxiUtils.rukuByDaohuoAndDaohuoDetail(lvDaohuo,entity, lvRuku);

        }
        // 审核入库 到货 形成应付账款
        financeApService.addByStoreIn(lvRuku);
        return list.size();
    }

    @Override
    @Transactional
    public int rukuByDaohuoDetail(String[] ids) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String today = sdf.format(new Date());
        String codeMaxString = lvRukumingxiService.selectMaxCode(today);
        if (StringUtils.isNotBlank(codeMaxString)) {
            // 最大编号+1
            codeMaxString = Long.parseLong(codeMaxString) + 1 + "";
        } else {
            codeMaxString = today + "0001";
        }
        LvRuku lvRuku = new LvRuku();
        lvRuku.setNos(codeMaxString);
        lvRuku.setState(BusinessConstants.SALE_TYPE_9);
        lvRuku.setCreateBy(SecurityUtils.getUsername());
        lvRuku.setCreateTime(DateUtils.getNowDate());
        lvRuku.setRukuDate(new Date());
        lvRuku.setRukuid(SecurityUtils.getUserId());
        lvRuku.setRukupeople(SecurityUtils.getUsername());
        lvRuku.setType(BusinessConstants.TYPE_RUKU);
        lvRuku.setId(IdUtils.fastSimpleUUID());
        lvRukuMapper.insert(lvRuku);
        int i = 1;
        try {
            for (String id : ids) {
                LvDaohuoDetail entity = lvDaohuoDetailMapper.selectById(id);
                RukumingxiUtils.rukuByDaohuoDetail(entity, lvRuku);
            }
        } catch (Exception e) {
            i = 0;
            return ids.length;
        }
        return ids.length;
    }


}
